package cn.golxen.goods.controller;


import cn.golxen.commons.entity.Result;
import cn.golxen.commons.exception.GoodsDeleteException;
import cn.golxen.commons.utils.TokenGetUID;
import cn.golxen.commons.vo.BarcodeInfoVo;
import cn.golxen.commons.vo.CounterVO;
import cn.golxen.commons.vo.GoodsImageVo;
import cn.golxen.goods.entity.Category;
import cn.golxen.goods.entity.Goods;
import cn.golxen.goods.service.CategoryService;
import cn.golxen.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    GoodsService goodsService;



    @GetMapping(value = "/scan")
    public Result ScanEvents(@RequestParam(value = "url") String url,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        return goodsService.ScanEvents(url,userId);
    }

    @GetMapping(value = "/all")
    public Result getGoodsList(HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        return Result.successResult(goodsService.getGoodsList(userId));
    }

    @GetMapping(value = "/category")
    public Result selectCategory(HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        List<Category> list=categoryService.selectCategory();
        return Result.successResult(list);
    }

    @GetMapping(value = "/getcategory/{id}")
    public Result selectCategoryByCategoryId(@PathVariable(value = "id")Integer id,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Category category=categoryService.selectCategoryByCategoryId(id);
        return Result.successResult(category);
    }

    @GetMapping(value = "/category/{id}")
    public Result selectGoodsByCategoryId(@PathVariable(value = "id") Integer id,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        List<Goods> list=goodsService.selectGoodsByCategoryId(id,userId);
        return Result.successResult(list);
    }

//    @GetMapping(value = "/barcode/{code}")
//    public Result getGoodsByBarcode(@PathVariable(value = "code") String code){
//        BarcodeInfoVo barcodeInfoVo=goodsService.getGoodsByBarcode(code);
//        return Result.successResult(barcodeInfoVo);
//    }

    @GetMapping(value = "/{code}")
    public Result getGoodsByCode(@PathVariable(value = "code") String code,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Goods goods=goodsService.getGoodsByCode(code,userId);
        return Result.successResult(goods);
    }

    @PostMapping(value = "/add")
    public Result addGoods(@RequestBody Goods goods,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Goods resGoods=goodsService.addGoods(goods,userId);
        return Result.successResult(resGoods);

    }

    @PostMapping(value = "/update")
    public Result updateGoods(@RequestBody Goods goods,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Goods resGoods=goodsService.updateGoods(goods,userId);
        return Result.successResult(resGoods);
    }

    @PostMapping(value="/delete")
    public Result deleteGoods(@RequestBody List<String> code,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        if (goodsService.deleteGoods(code,userId)==0)
            throw new GoodsDeleteException();
        else
            return new Result(200,"删除成功");
    }

    @GetMapping(value = "/name/{name}")
    public Result selectGoodsByGoodsName(@PathVariable(value = "name")String name,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        List<Goods> list=goodsService.selectGoodsByGoodsName(name,userId);
        return Result.successResult(list);
    }

    @PostMapping(value = "/uploader")
    public Result updateGoodsImage(@RequestBody GoodsImageVo goodsImageVo,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Integer integer=goodsService.updateGoodsImage(goodsImageVo,userId);
        return Result.successResult(integer);
    }

    @PostMapping(value = "/sell")
    public Result updateGoodsInventory(@RequestBody List<CounterVO> counterVO ,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        Integer integer=goodsService.updateGoodsInventory(counterVO,userId);
        if (integer!=0){
            return Result.successResult(integer);
        }else
            return new Result(10,"修改失败");

    }
    @GetMapping(value = "/getGoodss/{code}")
    public Result getGoodssByCode(@PathVariable (value = "code") String code,HttpServletRequest request){
        String userId= TokenGetUID.getId(request.getHeader("token"));
        List<Goods> list=goodsService.getGoodssByCode(code,userId);
        return Result.successResult(list);
    }
}
